'use strict';

angular.module('QC').controller('QCStandardController',
    ['$scope', '$stateParams', 'BaseModule', 'BaseFunction', 'ExtendEnumService', 'QCEnum', '$location', 'Authentication',
        function ($scope, $stateParams, BaseModule, BaseFunction, ExtendEnumService, QCEnum, $location, Authentication) {
            $scope.SeineEnum = {
                Value1: {Value: 1, Text: '是'},
                Value0: {Value: 0, Text: '否'}
            };
            $scope.ItemCategoryEnum;
            ExtendEnumService.getExtendEnum('ItemCategory', function(extendEnum){
                $scope.ItemCategoryEnum = extendEnum;
            });

            $scope.TypeEnum = BaseFunction.getEnum('QCEnum.QCNormType');
            $scope.btnSave = function () {
                if(($scope.Module.QCStandard.FocusedRecord.ItemMaster == undefined || 
                    $scope.Module.QCStandard.FocusedRecord.ItemMaster == null) && 
                    ($scope.Module.QCStandard.FocusedRecord.ItemCategory === undefined || 
                    $scope.Module.QCStandard.FocusedRecord.ItemCategory == null)){
                    BaseFunction.alert("料品和分类必须录入一个", "保存提示");
                    return;
                }
                if($scope.Module.QCStandard.FocusedRecord.Work == undefined || 
                    $scope.Module.QCStandard.FocusedRecord.Work == null){
                    BaseFunction.alert("请录入作业", "保存提示");
                    return;
                }
                if($scope.Module.QCStandard.FocusedRecord.QCNorm == undefined || 
                    $scope.Module.QCStandard.FocusedRecord.QCNorm == null){
                    BaseFunction.alert("请录入质检指标", "保存提示");
                    return;
                }

                if($scope.Module.QCStandard.FocusedRecord.QCNorm.Type == QCEnum.QCNormType.Number.Value){
                    if($scope.Module.QCStandard.FocusedRecord.ValueLowerLimit == undefined || 
                        $scope.Module.QCStandard.FocusedRecord.ValueLowerLimit == null){
                        BaseFunction.alert("当指标类型为数值时，标准下限值不能为空", "保存提示");
                        return;
                    }
                    if($scope.Module.QCStandard.FocusedRecord.ValueUpperLimit == undefined || 
                        $scope.Module.QCStandard.FocusedRecord.ValueUpperLimit == null){
                        BaseFunction.alert("当指标类型为数值时，标准上限值不能为空", "保存提示");
                        return;
                    }
                    if($scope.Module.QCStandard.FocusedRecord.ValueUpperLimit < $scope.Module.QCStandard.FocusedRecord.ValueLowerLimit){
                        BaseFunction.alert("标准值上限不能小于标准值下限", "保存提示");
                        return;
                    }
                    if($scope.Module.QCStandard.FocusedRecord.StandardValueNumber != null && $scope.Module.QCStandard.FocusedRecord.StandardValueNumber != undefined){
                        $scope.Module.QCStandard.FocusedRecord.StandardValue = $scope.Module.QCStandard.FocusedRecord.StandardValueNumber.toString();
                    }
                    else{
                        $scope.Module.QCStandard.FocusedRecord.StandardValue = null;
                    }
                }
                else if($scope.Module.QCStandard.FocusedRecord.QCNorm.Type == QCEnum.QCNormType.String.Value){
                    $scope.Module.QCStandard.FocusedRecord.StandardValue = $scope.Module.QCStandard.FocusedRecord.StandardValueText;
                }
                else if($scope.Module.QCStandard.FocusedRecord.QCNorm.Type == QCEnum.QCNormType.Bool.Value){
                    if($scope.Module.QCStandard.FocusedRecord.StandardValueCheck){
                        $scope.Module.QCStandard.FocusedRecord.StandardValue = "true";
                    }
                    else{
                        $scope.Module.QCStandard.FocusedRecord.StandardValue = "false";
                    }
                }
                else if($scope.Module.QCStandard.FocusedRecord.QCNorm.Type == QCEnum.QCNormType.Select.Value){
                    if($scope.Module.QCStandard.FocusedRecord.StandardValueSelect != null && $scope.Module.QCStandard.FocusedRecord.StandardValueSelect != undefined){
                        $scope.Module.QCStandard.FocusedRecord.StandardValue = $scope.Module.QCStandard.FocusedRecord.StandardValueSelect.toString();
                    }
                    else{
                        $scope.Module.QCStandard.FocusedRecord.StandardValue = null;
                    }
                }

                $scope.Module.QCStandard.save(function(){
                    if($scope.Module.QCStandard.FocusedRecord.StandardValue != null && $scope.Module.QCStandard.FocusedRecord.StandardValue != undefined){
                        if($scope.Module.QCStandard.FocusedRecord.QCNorm.Type == QCEnum.QCNormType.Number.Value){
                            $scope.Module.QCStandard.FocusedRecord.StandardValueNumber = parseFloat($scope.Module.QCStandard.FocusedRecord.StandardValue);
                        }
                        else if($scope.Module.QCStandard.FocusedRecord.QCNorm.Type == QCEnum.QCNormType.String.Value){
                            $scope.Module.QCStandard.FocusedRecord.StandardValueText = $scope.Module.QCStandard.FocusedRecord.StandardValue;
                        }
                        else if($scope.Module.QCStandard.FocusedRecord.QCNorm.Type == QCEnum.QCNormType.Bool.Value){
                            $scope.Module.QCStandard.FocusedRecord.StandardValueCheck = $scope.Module.QCStandard.FocusedRecord.StandardValue == "true" ? true : false;
                        }
                        else if($scope.Module.QCStandard.FocusedRecord.QCNorm.Type == QCEnum.QCNormType.Select.Value){
                            $scope.Module.QCStandard.FocusedRecord.StandardValueSelect = parseInt($scope.Module.QCStandard.FocusedRecord.StandardValue);
                        }
                    }
                });
            };
            $scope.btnCancel = function () {
                $scope.Module.QCStandard.cancel();
            };
            $scope.btnAdd = function () {
                $scope.Module.QCStandard.add();
            };
            $scope.btnDelete = function () {
                $scope.Module.QCStandard.delete();
            };
            $scope.btnList = function () {
                $location.url('/index/QCStandard/List');
            };

            $scope.QCNormExtendEnum;
            $scope.qcNormChange = function(records){
                $scope.QCNormExtendEnum = null;
                records[0].StandardValue = "";
                records[0].StandardValueNumber = "";
                records[0].StandardValueText = "";
                records[0].StandardValueCheck = false;
                records[0].StandardValueSelect = null;
                records[0].ValueUpperLimit = "";
                records[0].ValueLowerLimit = "";

                if(records.length > 0 && records[0].Type == QCEnum.QCNormType.Select.Value){
                    $scope.QCNormExtendEnum = {};
                    for(var i=0; i<records[0].ExtendEnum.Options.length; i++){
                        $scope.QCNormExtendEnum["Enum" + i.toString()] = {
                            Value: records[0].ExtendEnum.Options[i].Value, 
                            Text: records[0].ExtendEnum.Options[i].Name
                        }
                    }
                }
            };

            //初始化
            BaseModule.initModule($scope,'QCStandard');
            $scope.Module.QCStandard.Populate = [
                {path: 'ItemMaster', select: 'Code Name'},
                {path: 'Work', select: 'Code Name'},
                {path: 'QCNorm', select: 'Code Name Type ExtendEnum', populate: {path: "ExtendEnum", model: 'ExtendEnum'}}
            ];
            $scope.Module.QCStandard.AfterOnLoad = function(records){
                $scope.QCNormExtendEnum = null;
                records[0].StandardValueNumber = "";
                records[0].StandardValueText = "";
                records[0].StandardValueCheck = false;
                records[0].StandardValueSelect = null;
                records[0].DescSeg1 = (records[0].DescSeg1 == "1" ? 1 : 0);


                if(records[0].QCNorm.Type == QCEnum.QCNormType.Select.Value){
                    $scope.QCNormExtendEnum = {};
                    for(var i=0; i<records[0].QCNorm.ExtendEnum.Options.length; i++){
                        $scope.QCNormExtendEnum["Enum" + i.toString()] = {
                            Value: records[0].QCNorm.ExtendEnum.Options[i].Value, 
                            Text: records[0].QCNorm.ExtendEnum.Options[i].Name
                        };
                    }
                }

                for(var i=0; i<records.length; i++){
                    if(records[i].StandardValue != null && records[i].StandardValue != undefined){
                        if(records[i].QCNorm.Type == QCEnum.QCNormType.Number.Value){
                            records[i].StandardValueNumber = parseFloat(records[i].StandardValue);
                        }
                        else if(records[i].QCNorm.Type == QCEnum.QCNormType.String.Value){
                            records[i].StandardValueText = records[i].StandardValue;
                        }
                        else if(records[i].QCNorm.Type == QCEnum.QCNormType.Bool.Value){
                            records[i].StandardValueCheck = records[i].StandardValue == "true" ? true : false;
                        }
                        else if(records[i].QCNorm.Type == QCEnum.QCNormType.Select.Value){
                            records[i].StandardValueSelect = parseInt(records[i].StandardValue);
                        }
                    }
                }
            };

            if($stateParams.id){
                $scope.Module.QCStandard.findById($stateParams.id, function(record){
                    $scope.Module.QCStandard.AfterOnLoad([record]);
                    $scope.Module.QCStandard.FocusedRecord = record;
                })
            }
            else {
                $scope.btnAdd();
            };
            $scope.Menu='首页';
        }
    ]);
